package guitplugin;

import com.sun.mirror.declaration.TypeDeclaration;
import com.sun.mirror.type.PrimitiveType;
import com.sun.mirror.type.TypeMirror;
import com.sun.mirror.type.VoidType;

public class AptUtils {

  public static String getTypeQualifiedName(TypeMirror type, boolean allowPrimitives) {
    if (type instanceof TypeDeclaration) {
      return ((TypeDeclaration) type).getQualifiedName();
    } else if (type instanceof VoidType) {
      if (allowPrimitives) {
        return "void";
      } else {
        return Void.class.getCanonicalName();
      }
    } else if (type instanceof PrimitiveType) {
      String primitive = ((PrimitiveType) type).getKind().name().toLowerCase();
      if (allowPrimitives) {
        return primitive;
      } else {
        return "java.lang." + primitive.substring(0, 1).toUpperCase() + primitive.substring(1);
      }
    } else {
      throw new RuntimeException(type.getClass().getCanonicalName());
    }
  }

  public static String getProxyOrTypeQualifiedName(TypeMirror type, boolean allowPrimitives) {
    ProxyVisitor visitor = new ProxyVisitor();
    type.accept(visitor);
    if (allowPrimitives) {
      return visitor.getQualifiedName();
    } else {
      return visitor.getClassQualifiedName();
    }
  }
}
